//
//  TSPraiseEditContentController.m
//  chart
//
//  Created by 曾斌 on 16/4/14.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSCommitEditContentController.h"
#import "TSTaskModel.h"
#import "TSTools.h"
#import "FBFilesTableViewController.h"
@implementation TSCommitEditContentController
{
    TSTaskModel *taskModel;
}
@synthesize taskId,enclosurePath;


- (void)viewDidLoad
{
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedEnclosure:) name:TSNotification_Selected_Enclosure object:nil];
    UIButton * newView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    newView.frame =CGRectMake(0,400, currentDeviceWidth,100);
    [self.view addSubview:newView];
    [newView setTitle:@"选择附件" forState:UIControlStateNormal];
    [newView addTarget:self action:@selector(selectFile) forControlEvents:UIControlEventTouchUpInside];
}

-(void)selectedEnclosure:(NSNotification *)noti
{
    self.enclosurePath = noti.object;
}

-(void)selectFile
{
    NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    FBFilesTableViewController * ftView = [[FBFilesTableViewController alloc] initWithPath:docs[0]];
    ftView.vc_root = self;
    [self.navigationController pushViewController:ftView animated:YES];
}
-(void)initLeftButton{
    WS(weakSelf);
    [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_dark_close"] barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}
-(void) initTitle{
    self.title =@"提交内容";
    WS(weakSelf);
    [self rightBarButtonWithTitle:@"确认" barNorImage:nil barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        [weakSelf rightClick];
    }];
    
}

-(void)rightClick
{
    WS(weakSelf);
    taskModel = [[TSTaskModel alloc] init];
    taskModel.requestParams =@{TSDictionaryKeyDefine_Id:taskId,@"content": [self.textField text]};
    if(self.enclosurePath!=nil){
        [taskModel uploadFileWithPath:kRequestUrl_Task_Action_Submit filePath:self.enclosurePath completionHandler:^(BOOL isSucceeded, NSError *error) {
            if (!weakSelf) {
                return;
            }
            [TSTools showAlert:@"提交成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }else{
        
        [taskModel fetchWithPath:kRequestUrl_Task_Action_Submit completionHandler:^(BOOL isSucceeded, NSError *error) {
            if (!weakSelf) {
                return;
            }
            [TSTools showAlert:@"提交成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } NoProgress:NO];
    }

}

@end
